home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1995 November / EnigmA AMIGA RUN 02 (1995)(G.R. Edizioni)(IT)[!][issue 1995-11][Skylink CD].iso / earcd / util / text / crlf2lf.lha / CRLF2LF.c < prev    next >
C/C++ Source or Header  |  1995-09-06  |  2KB  |  96 lines

  1. */
  2. *  Original source by Krzysztof Sporysz vel Kivi 06-sep-95
  3. *  Small, Fast and simple CRLF to LF converter
  4. */
  5.  
  6. #include <dos/dos.h>
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <functions.h>
  10. #include <exec/exec.h>
  11. #define HELP "Usage: infile outfile"
  12. #define BUF 1024*32
  13. char __stdiowin[]= "CON:0/20/320/80/CRLF2LF v1.0 by Kiviks";
  14. char __stdiov37[]= "/AUTO/CLOSE/WAIT";
  15. BPTR infile,outfile;    
  16. char in[BUF+1];
  17. char out[BUF+1];
  18. int j;
  19. void error(int deep, char kom[])
  20. {
  21.     switch(deep)
  22.     {
  23.         case 2:
  24.         {
  25.             Close(outfile);
  26.         }
  27.         case 1:
  28.         {
  29.             Close(infile);
  30.         }
  31.         case 0:
  32.         {
  33.             printf("%s\n",kom);
  34.             exit(0);
  35.             break;
  36.         }
  37.     }
  38. }
  39. void convert(int leng)
  40. {
  41.     int i,offset=0;
  42.     for(i=0;(i+offset)<leng;i++)
  43.     {
  44.         if(in[i+offset]!=13)
  45.         {
  46.             out[j]=in[i+offset];
  47.             j++;
  48.             if(j>=BUF) Write(outfile,out,BUF),j=0;
  49.         }
  50.         else
  51.         {
  52.             if(in[i+offset+1]=10)
  53.             {
  54.                 out[j]=10;
  55.                 j++;
  56.                 if(j>=BUF)
  57.                 {
  58.                     Write(outfile,out,BUF);
  59.                     j=0;
  60.                 }
  61.                 offset++;
  62.             }
  63.         }
  64.     }
  65. }
  66. void main(int argc,char *argv[] )
  67. {
  68.     long bytes_read=BUF,i;
  69.     if(argc != 3) error(0,HELP);
  70.     printf("In File  :%s\nOut File :%s\n",argv[1],argv[2]);
  71.     
  72.     infile=Open(argv[1],MODE_OLDFILE);
  73.     if(!infile)    error(0,"In File couldn't be opened.");
  74.     
  75.     outfile=Lock(argv[2],EXCLUSIVE_LOCK);
  76.     if(outfile)
  77.     {
  78.         UnLock(outfile);
  79.         error(1,"Out File exist. Can't overwrite.");
  80.     }
  81.     outfile=Open(argv[2],MODE_NEWFILE);
  82.     j=0;
  83.     while(bytes_read==BUF)
  84.     {
  85.         bytes_read=Read(infile,in,BUF+1);
  86.         convert(bytes_read-1);
  87.         bytes_read--;
  88.         Seek(infile,-1,OFFSET_CURRENT);
  89.     }
  90.     if(j>0 && j<=BUF)
  91.     {
  92.         Write(outfile,out,j);
  93.     }
  94.     error(2,"Success !!!");
  95. }
  96.